/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.kapil.framework.concurrent.impl;

import com.kapil.framework.concurrent.IBroker;

/**
 * <p>
 * Base and <code>abstract</code> class any class that wants to implement {@link IBroker}. This class does not include
 * any capabilities to add or read data from the data storage. It simply holds the information like capacity, timeout
 * and provides common functions for checking status for production of code and also to start and stop it.
 * </p>
 * 
 * @author Kapil Viren Ahuja
 * 
 * @param <V>
 */
public abstract class AbstractBroker<V> implements IBroker<V>
{
    protected int   capacity;
    protected int   timeoutDuration;
    private Boolean isProducing = Boolean.TRUE;


    /**
     * Constructor with no parameters. It will initialize the {@link IBroker} with following settings:
     * 
     * <ul>
     * <li>Capacity: 100</li>
     * <li>Timeout: 3 seconds</li>
     * </ul>
     * 
     */
    public AbstractBroker()
    {
        this(100);
    }


    /**
     * Constructor that accepts the capacity as the parameter. It will initialize the {@link IBroker} with following
     * settings:
     * 
     * <ul>
     * <li>Capacity: as provided</li>
     * <li>Timeout: 3 seconds</li>
     * </ul>
     * 
     */
    public AbstractBroker(int capacity)
    {
        this(capacity, 3);
    }


    /**
     * Constructor with parameters for capacity and timeout (in seconds)
     * 
     * <ul>
     * <li>Capacity: 100</li>
     * <li>Timeout: 3 seconds</li>
     * </ul>
     * 
     */
    public AbstractBroker(int capacity, int timeoutDuration)
    {
        this.capacity = capacity;
        this.timeoutDuration = timeoutDuration;
    }


    /**
     * see {@link IBroker#put(Object)}
     * 
     */
    public abstract void put(V data) throws InterruptedException;


    /**
     * see {@link IBroker#get()}
     * 
     */
    public abstract V get() throws InterruptedException;


    /**
     * see {@link IBroker#isProducing()}
     * 
     */
    public Boolean isProducing()
    {
        return isProducing;
    }


    /**
     * see {@link IBroker#stopProducing()}
     * 
     */
    public void stopProducing()
    {
        this.isProducing = Boolean.FALSE;
    }


    /**
     * see {@link IBroker#startProducing()}
     * 
     */
    public void startProducing()
    {
        this.isProducing = Boolean.TRUE;
    }
}
